package com.example.chatgptapplication.controller.admin;

import com.example.chatgptapplication.common.Constrants;
import com.example.chatgptapplication.entity.User;

import com.example.chatgptapplication.enums.ResponseStatus;

import com.example.chatgptapplication.service.UserService;
import com.example.chatgptapplication.utils.ImageUtil;
import com.example.chatgptapplication.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService  userService;


    @Autowired
    private ImageUtil imageUtil;


    @PostMapping("/createUser")
    public ResponseData createUser(@RequestBody User user){
        userService.createUser(user);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }


    @PostMapping("/updateUser")
    public ResponseData updateUser(@RequestBody User user){
        userService.updateUser(user);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

    @PostMapping("/deleteUser")
    public ResponseData deleteUser(@RequestBody User user){
        userService.removeById(user.getId());
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

    @PostMapping("/selectUserPageData")
    public ResponseData selectUserPageData(@RequestBody User user){
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), userService.getUserPageData(user));
    }


    @PostMapping("/addUserAvatar")
    public ResponseData addUserAvatar(@RequestBody MultipartFile file){
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), imageUtil.uploadFile(file, Constrants.USERAVTARFOLDER,"user"));
    }

    @PostMapping("/forbidden")
    public  ResponseData forbidden(@RequestBody User user){
        userService.forbidden(user);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

    @PostMapping("/enable")
    public ResponseData enable(@RequestBody User user){
        userService.enable(user);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

}
